package com.fivefu.core.hik.services.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.core.hik.constant.HikRequestTypeEnum;
import com.fivefu.core.hik.dto.request.*;
import com.fivefu.core.hik.dto.response.*;
import com.fivefu.core.hik.entity.DbHikRegions;
import com.fivefu.core.hik.services.DbHikRegionsService;
import com.fivefu.core.hik.services.HikvisionService;
import com.fivefu.core.hik.services.RegionServices;
import com.fivefu.core.hik.vo.RegionsTimeRangeVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class RegionServicesImpl implements RegionServices {

    @Resource
    private DbHikRegionsService dbHikRegionsService;

    @Resource
    private HikvisionService hikvisionService;

    @Override
    public void queryRegionsRoot() {
        getRegionsRoot();
    }

    @Override
    public void queryHikRegionsList(String resourceType) {
        //当前页
        Integer pageNo = 1;
        //每页显示条数
        Integer pageSize = 1000;

        getHikRegionsList(pageNo,pageSize,resourceType);
    }

    @Override
    public void queryHikRegionsByPage() {
        //当前页
        Integer pageNo = 1;
        //每页显示条数
        Integer pageSize = 1000;
        List<String> regionCodeList = new ArrayList<>();
        getHikRegionsByPage(pageNo,pageSize,regionCodeList);
        //区域信息同步完成，删除多余的数据信息
        dbHikRegionsService.deleteHikRegion(regionCodeList,"sys");
    }

    @Override
    public void queryHikSubRegionsList() {
        //当前页
        Integer pageNo = 1;
        //每页显示条数
        Integer pageSize = 1000;
        String resourceType = "region";
        getHikSubRegionsList(pageNo,pageSize,resourceType);
    }

    @Override
    public void queryHikRegionsDetail() {
        getHikRegionsDetail();
    }

    @Override
    public void queryHikRegionsTimeRange(RegionsTimeRangeVo regionsTimeRangeVo) {
        Integer pageNo = regionsTimeRangeVo.getPageNo();
        Integer pageSize = regionsTimeRangeVo.getPageSize();
        String startTime = regionsTimeRangeVo.getStartTime();
        String endTime = regionsTimeRangeVo.getEndTime();
        getHikRegionsTimeRange(startTime,endTime,pageNo,pageSize);
    }

    /**
     * 增量获取区域数据
     * @param startTime
     * @param endTime
     * @param pageNo
     * @param pageSize
     */
    private void getHikRegionsTimeRange(String startTime, String endTime, Integer pageNo, Integer pageSize) {
        RegionsTimeRangeReq req = new RegionsTimeRangeReq(startTime, endTime, pageNo, pageSize);
        String body = JSON.toJSONString(req);
        //查询枚举类型
        HikRequestTypeEnum typeEnum = HikRequestTypeEnum.REGIONINCREMENT;
        //调用接口
        ResponseResult result = hikvisionService.doPostString(typeEnum.getUrl(), body, null, null, typeEnum.getType());
        if (result != null && "0".equals(result.getCode())) {//调用成功
            String resultData = result.getData().toString();
            if (StrUtils.isNotNull(resultData)){
                ResponsePage resultPage = JSON.parseObject(resultData, ResponsePage.class);
                resultPage.getList().forEach(o -> {
                    RegionsByParamsResp resp = JSON.parseObject(JSON.toJSONString(o), RegionsByParamsResp.class);
                    DbHikRegions regions = resp.replaceDbHikRegions();
                    //编辑区域信息
                    dbHikRegionsService.editHikRegion(regions);
                });
            }
        }
    }

    /**
     * 根据编号获取区域详细信息
     */
    private void getHikRegionsDetail() {
        List<String> indexCodes = dbHikRegionsService.queryRegionIDs();
        for (String indexCode : indexCodes) {
            List<String> list = new ArrayList<>();
            list.add(indexCode);
            RegionInfoReq req = new RegionInfoReq(list);
            String body = JSON.toJSONString(req);
            //查询枚举类型
            HikRequestTypeEnum typeEnum = HikRequestTypeEnum.REGIONDETAIL;

            //调用接口
            ResponseResult result = hikvisionService.doPostString(typeEnum.getUrl(), body, null, null, typeEnum.getType());
            if (result != null && "0".equals(result.getCode())){//调用成功
                String resultData = result.getData().toString();
                if (StrUtils.isNotNull(resultData)){
                    ResponsePage resultPage = JSON.parseObject(resultData, ResponsePage.class);
                    resultPage.getList().forEach(o -> {
                        RegionsByParamsResp resp = JSON.parseObject(JSON.toJSONString(o), RegionsByParamsResp.class);
                        DbHikRegions regions = resp.replaceDbHikRegions();
                        //编辑区域信息
                        dbHikRegionsService.editHikRegion(regions);
                    });
                }
            }
        }

    }

    /**
     * 根据区域编号获取下一级区域列表v2
     * @param pageNo 当前页
     * @param pageSize 每页显示条数
     * @param resourceType 资源类型
     */
    private void getHikSubRegionsList(Integer pageNo, Integer pageSize, String resourceType) {
        List<String> ids = dbHikRegionsService.queryRegionParentIds();
        for (String parentIndexCode : ids) {
            //分页参数
            RegionsReq req = new RegionsReq(parentIndexCode,resourceType,pageNo,pageSize);
            String body = JSON.toJSONString(req);

            //查询枚举类型
            HikRequestTypeEnum typeEnum = HikRequestTypeEnum.REGIONSUBLIST;

            //调用接口
            ResponseResult result = hikvisionService.doPostString(typeEnum.getUrl(), body, null, null, typeEnum.getType());
            if (result != null && "0".equals(result.getCode())){//调用成功
                String resultData = result.getData().toString();
                if (StrUtils.isNotNull(resultData)){
                    ResponsePage resultPage = JSON.parseObject(resultData, ResponsePage.class);
                    resultPage.getList().forEach(o -> {
                        RegionsByParamsResp resp = JSON.parseObject(JSON.toJSONString(o), RegionsByParamsResp.class);
                        DbHikRegions regions = resp.replaceDbHikRegions();
                        //编辑区域信息
                        dbHikRegionsService.editHikRegion(regions);
                    });
                }
            }
        }
    }

    /**
     * 分页获取区域列表
     * @param pageNo
     * @param pageSize
     * @param regionCodeList
     */
    private void getHikRegionsByPage(Integer pageNo, Integer pageSize, List<String> regionCodeList) {
        //分页参数
        PageReq pageReq = new PageReq(pageNo,pageSize);
        String body = JSON.toJSONString(pageReq);

        //查询枚举类型
        HikRequestTypeEnum typeEnum = HikRequestTypeEnum.REGIONPAGELIST;
        //调用接口
        ResponseResult result = hikvisionService.doPostString(typeEnum.getUrl(), body, null, null, typeEnum.getType());
        if (result != null && "0".equals(result.getCode())){
            String resultData = result.getData().toString();
            if (StrUtils.isNotNull(resultData)){
                ResponsePage resultPage = JSON.parseObject(resultData, ResponsePage.class);
                resultPage.getList().forEach(o ->{
                    RegionsResp resp = JSON.parseObject(JSON.toJSONString(o), RegionsResp.class);
                    DbHikRegions regions = resp.replaceDbHikRegions();
                    //编辑区域信息
                    dbHikRegionsService.editHikRegion(regions);
                    regionCodeList.add(regions.getIndexCode());
                });

                //循环查询分页数据
                if (resultPage.getTotal() > ((pageNo - 1) * pageSize + pageSize)){
                    getHikRegionsByPage( pageNo + 1, pageSize, regionCodeList);
                }
            }
        }

    }

    /**
     * 查询海康区域列表v2
     * @param pageNo 当前页
     * @param pageSize 每页显示条数
     * @param resourceType 资源类型
     */
    private void getHikRegionsList(Integer pageNo,Integer pageSize,String resourceType) {
        RegionsReq req = new RegionsReq(resourceType,pageNo,pageSize);
        //查询海康区域列表v2 必要参数
        String body = JSON.toJSONString(req);

        //查询类型枚举
        HikRequestTypeEnum typeEnum = HikRequestTypeEnum.REGIONLIST;

        //调用接口
        ResponseResult result = hikvisionService.doPostString(typeEnum.getUrl(), body, null, null, typeEnum.getType());
        if (result!=null && "0".equals(result.getCode())){
            String resultData = result.getData().toString();
            if (StrUtils.isNotNull(resultData)){
                ResponsePage resultPage = JSON.parseObject(resultData, ResponsePage.class);

                resultPage.getList().forEach(o -> {
                    RegionsByParamsResp resp = JSON.parseObject(JSON.toJSONString(o),RegionsByParamsResp.class);
                    DbHikRegions regions = resp.replaceDbHikRegions();
                    //编辑监控点信息
                    dbHikRegionsService.editHikRegion(regions);
                });

                //循环查询分页数据
                if (resultPage.getTotal() > ((pageNo - 1) * pageSize + pageSize)){
                    getHikRegionsList( pageNo + 1, pageSize,resourceType);
                }
            }
        }

    }

    /**
     * 获取根区域信息
     */
    public void getRegionsRoot(){
        JSONObject obj = new JSONObject();
        String body = JSON.toJSONString(obj);

        //查询类型枚举
        HikRequestTypeEnum typeEnum = HikRequestTypeEnum.REGIONROOT;

        //调用接口
        ResponseResult result = hikvisionService.doPostString(typeEnum.getUrl(), body, null, null, typeEnum.getType());
        if (result != null && "0".equals(result.getCode())){//调用成功
            String resultData = result.getData().toString();
            if (StrUtils.isNotNull(resultData)){
                RegionsResp resp = JSON.parseObject(resultData,RegionsResp.class);
                DbHikRegions regions = resp.replaceDbHikRegions();
                //编辑区域信息
                dbHikRegionsService.editHikRegion(regions);
            }
        }
    }


    /**
     * 分页获取区域列表
     * @param pageNo
     * @param pageSize
     */
    @Override
    public List<DbHikRegions> getHikRegionsAllByPage(Integer pageNo, Integer pageSize) {
        List<DbHikRegions> regionsList = new ArrayList<>();
        //分页参数
        PageReq pageReq = new PageReq(pageNo,pageSize);
        String body = JSON.toJSONString(pageReq);

        //查询枚举类型
        HikRequestTypeEnum typeEnum = HikRequestTypeEnum.REGIONPAGELIST;
        //调用接口
        ResponseResult result = hikvisionService.doPostString(typeEnum.getUrl(), body, null, null, typeEnum.getType());
        if (result != null && "0".equals(result.getCode())){
            String resultData = result.getData().toString();
            if (StrUtils.isNotNull(resultData)){
                ResponsePage resultPage = JSON.parseObject(resultData, ResponsePage.class);
                resultPage.getList().forEach(o ->{
                    RegionsResp resp = JSON.parseObject(JSON.toJSONString(o), RegionsResp.class);
                    DbHikRegions regions = resp.replaceDbHikRegions();
                    regionsList.add(regions);
                });

                //循环查询分页数据
                if (resultPage.getTotal() > ((pageNo - 1) * pageSize + pageSize)){
                    getHikRegionsAllByPage( pageNo + 1, pageSize);
                }
            }
        }
        return regionsList;
    }

    /**
     * 根据编号获取区域详细信息(汇总用的)
     */
    @Override
    public List<DbHikRegions> getHikRegionDetailAll(List<String> resultList) {
        List<DbHikRegions> regionsList = new ArrayList<>();
        for (String indexCode : resultList) {
            List<String> list = new ArrayList<>();
            list.add(indexCode);
            RegionInfoReq req = new RegionInfoReq(list);
            String body = JSON.toJSONString(req);
            //查询枚举类型
            HikRequestTypeEnum typeEnum = HikRequestTypeEnum.REGIONDETAIL;
            //调用接口
            ResponseResult result = hikvisionService.doPostString(typeEnum.getUrl(), body, null, null, typeEnum.getType());
            if (result != null && "0".equals(result.getCode())){//调用成功
                String resultData = result.getData().toString();
                if (StrUtils.isNotNull(resultData)){
                    ResponsePage resultPage = JSON.parseObject(resultData, ResponsePage.class);
                    resultPage.getList().forEach(o -> {
                        RegionsByParamsResp resp = JSON.parseObject(JSON.toJSONString(o), RegionsByParamsResp.class);
                        DbHikRegions regions = resp.replaceDbHikRegions();
                        regionsList.add(regions);
                    });
                }
            }
        }
        return regionsList;
    }

}
